const path = require('path')
const {
  navList,
  foreshop,
  allkill,
  homeData,
  homeList,
  cheapList,
  shopticket,
  showsList,
  showList,
  groupList
} = require('./src/mock/data')
const { userData } = require('./src/mock/mock')
module.exports = {
  lintOnSave: false,
  devServer: {
    port: 4000,
    /* proxy: {
      '/api': {
        target: 'http://localhost:9004', //设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true, //這裡true表示实现跨域
        pathRewrite: {
          '^/api': '', //这里理解成用‘/api’代替target里面的地址，后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add'，直接写‘/api/user/add’即可
        },
      },
    }, */
    /* proxy: 'http://localhost:8080', */
    before(app) {
      //秒杀
      app.get('/foreshop', (req, res) => {
        res.send(foreshop)
      })
      app.get('/allkill', (req, res) => {
        res.send(allkill)
      })
      //首页
      app.get('/homeData', (req, res) => {
        res.send(homeData)
      })
      app.get('/homeList', (req, res) => {
        res.send(homeList)
      })
      app.get('/showList', (req, res) => {
        res.send(showList)
      })
      app.get('/showsList', (req, res) => {
        res.send(showsList)
      })
      //砍价详情
      app.get('/cheapList', (req, res) => {
        res.send(cheapList)
      })
      //我的优惠券
      app.get('/shopticket', (req, res) => {
        res.send(shopticket)
      })
      app.get('/groupList',(req,res)=>{
        res.send(groupList)
      })
      //登录
      app.get('/login', (req, res) => {
        //取出传输过来的值
        const { username, password } = req.query
        const bool = userData.some(
          (item) =>
            item.user + '' === username + '' && item.pass + '' === password + ''
        )
        if (bool) {
          res.send({
            code: 1,
            data: {
              token: '131415926',
              username,
            },
          })
        } else {
          res.send({
            code: -1,
            message: '请检查账号密码',
          })
        }
      })
      app.get('/navlist', (req, res) => {
        res.send(navList)
      })
    },
  },
  // webpack 配置进行更细粒度的修改  https://cli.vuejs.org/zh/config/#chainwebpack
  chainWebpack: (config) => {
    //修改文件引入自定义路径
    config.resolve.alias
      .set('@', path.resolve(__dirname, './src'))
      .set('@c', path.resolve(__dirname, './src/components'))
      .set('@css', path.resolve(__dirname, './src/assets/css'))
      .set('@img', path.resolve(__dirname, './src/assets/images'))
      .set('@js', path.resolve(__dirname, './src/assets/js'))
    // 1. 修改svg规则，排除icons目录下的svg文件
    config.module
      .rule('svg')
      // 排除icon目录
      // exclude 排除 add 向数组里插入值
      .exclude.add(path.resolve(__dirname + '/src/icons'))
    // 2. 新增规则，仅打包icons目录下的svg
    config.module
      .rule('icons') // 规则名称 有则取 无则添加
      .test(/\.svg$/) // 匹配规则
      // 使用了add之后 会离开当前链的上下文 所以需要end再返回上一步
      .include.add(path.resolve(__dirname + '/src/icons')) // 仅匹配icons 下的 svg
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
  },
}
